package xin.yangshuai.activemqgeneral.jms.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * JmsConsumerTopic3
 *
 * @author shuai
 * @date 2020/6/21
 */
public class JmsConsumerTopic3 {

    public static final String ACTIVEMQ_URL = "tcp://192.168.124.7:61616";
    public static final String TOPIC_NAME = "topic01";

    public static void main(String[] args) throws JMSException, IOException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        MessageConsumer messageConsumer = session.createConsumer(session.createTopic(TOPIC_NAME));

        messageConsumer.setMessageListener(message -> {
            if (message != null & message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                try {
                    System.out.println("****TOPIC消费者3接收消息-MessageListener：" + textMessage.getText());
                } catch (JMSException e) {
                    e.printStackTrace();
                }

            }
        });
        System.in.read();

        messageConsumer.close();
        session.close();
        connection.close();
    }
}
